package reviewAlgorithms.search;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Permute {
    public static void main(String[] args) {
        int[] nums = {1,2,3};
        List<List<Integer>> res = permute(nums);
        for (List<Integer> item: res){
            System.out.println(item);
        }
    }
    public static List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums == null || nums.length < 1){
            return res;
        }
        List<Integer> oneRes = new ArrayList<>();
        getPermute(nums,oneRes,res);

        return res;
    }

    private static void getPermute(int[] nums, List<Integer> oneRes, List<List<Integer>> res) {
        if (nums == null || nums.length < 1){
            res.add(new ArrayList<>(oneRes));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            oneRes.add(nums[i]);
            int temp = nums[0];
            nums[0] = nums[i];
            nums[i] = temp;
            getPermute(Arrays.copyOfRange(nums, 1,nums.length),oneRes,res);
            temp = nums[0];
            nums[0] = nums[i];
            nums[i] = temp;
            oneRes.remove(oneRes.size() - 1);
        }


    }
}
